﻿function normalProvider($httpProvider) {
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';


    var param = function (obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

        for (name in obj) {
            value = obj[name];

            if (value instanceof Array) {
                for (i = 0; i < value.length; i++) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if (value instanceof Object) {
                for (subName in value) {
                    subValue = value[subName];
                    fullSubName = name + '[' + subName + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if (value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }

        return query.length ? query.substr(0, query.length - 1) : query;
    };
    $httpProvider.defaults.transformRequest = [function (data) { return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data; }];
};

var Util = {};
Util.HttpGet = function ($http, url, callback) {
    layer.load();
    $http.get(url)
         .success(callback)
         .error(function (response) {
             Util.ShowMessage('error', response);
         });
    layer.closeAll('loading');
};
Util.HttpPost = function ($http, url, data, callback) {
    layer.load();
    $http.post(url, data)
         .success(callback)
         .error(function (response) {
             Util.ShowMessage('error', response);
         });
    layer.closeAll('loading');
};
Util.HttpDelete = function ($http, url) {
    layer.load();
    $http({
        method: "DELETE",
        url: url
    });
    layer.closeAll('loading');
};
//在数组中查找一个项，返回其索引
Util.FindInArray = function (arr, text) {
    if (arr.length <= 0) return -1;
    if (text == "") return -1;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == "") continue;
        if (arr[i] == text) return i;
    }
    return -1;
}
//显示提示信息
Util.ShowMessage = function (type, str) {
    if (type == "error") {
        top.toastr.error(str, '提示');
    } else {
        top.toastr.success(str, '提示');
    }

};

//去除字符串尾部空格或指定字符
String.prototype.TrimEnd = function (c) {
    if (c == null || c == "") {
        var str = this;
        var rg = /\/s/;
        var i = str.length;
        while (rg.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    }
    else {
        var str = this;
        var rg = new RegExp(c);
        var i = str.length;
        while (rg.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    }
};
